cmake_minimum_required(VERSION 3.10)

# 设置C++标准版本等
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_VERBOSE_MAKEFILE on)

#message("CMAKE_SYSTEM_NAME: ${CMAKE_SYSTEM_NAME}")
message("CMAKE_HOST_SYSTEM_NAME: ${CMAKE_HOST_SYSTEM_NAME}")

# 平台检测，或者指定编译工具链等
if(CMAKE_HOST_SYSTEM_NAME MATCHES "Linux")
    set(IS_LINUX 1)
else()
    #默认为windows的mingw64平台
    set(IS_WINDOWS 1)
endif()

project(NetworkTest)

# 编译的版本
#set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_BUILD_TYPE Release)

# 添加编译选项等
add_compile_options(-fPIC)

# 包含目录
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/inc)
include_directories(${PROJECT_SOURCE_DIR}/thirdparty/inc)
include_directories(${PROJECT_SOURCE_DIR}/thirdparty/inc/curl)

# 包含库
link_directories(${PROJECT_SOURCE_DIR}/bin)
if(IS_WINDOWS)
    # Windows平台
    link_directories(${PROJECT_SOURCE_DIR}/thirdparty/lib/wingw64)
else(IS_LINUX)
    # Linux平台
    link_directories(${PROJECT_SOURCE_DIR}/thirdparty/lib/linux)
endif()

# 设置目标输出路径等
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

# 添加库源文件和头文件
file(GLOB_RECURSE SRC_LIST
    ${PROJECT_SOURCE_DIR}/src/*.cpp
    ${PROJECT_SOURCE_DIR}/src/*.cc
    ${PROJECT_SOURCE_DIR}/src/*.cxx
    ${PROJECT_SOURCE_DIR}/src/*.c
    ${PROJECT_SOURCE_DIR}/src/*.h
    ${PROJECT_SOURCE_DIR}/src/*.hpp)

file(GLOB_RECURSE HEAD_LIST
    ${PROJECT_SOURCE_DIR}/inc/*.h
    ${PROJECT_SOURCE_DIR}/inc/*.hpp)

# 添加可执行行程序
add_executable(${PROJECT_NAME} ${HEAD_LIST} ${SRC_LIST})

# ubuntu环境需要安装curl库
# sudo apt install curl libcurl4-openssl-dev vsftpd -y

# 配置链接库
if(IS_WINDOWS)
    # Windows平台
    file(GLOB_RECURSE ThirdPartyLibs ${PROJECT_SOURCE_DIR}/thirdparty/lib/wingw64/*.dll)
    target_link_libraries(${PROJECT_NAME} ${ThirdPartyLibs} curl-4  ws2_32 pthread m)
else()
    # Linux平台
    file(GLOB_RECURSE ThirdPartyLibs ${PROJECT_SOURCE_DIR}/thirdparty/lib/linux/*.so)
    target_link_libraries(${PROJECT_NAME} ${ThirdPartyLibs} curl dl rt pthread)
endif()

# 安装配置
if(IS_WINDOWS)
    # Windows平台
    install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/wingw64/ DESTINATION ${EXECUTABLE_OUTPUT_PATH})
else()
    # Linux平台
    install(DIRECTORY ${PROJECT_SOURCE_DIR}/thirdparty/lib/linux/ DESTINATION ${EXECUTABLE_OUTPUT_PATH})
endif()

# 配置编译清理指令
add_custom_target(clean-all
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target clean
    COMMAND ${CMAKE_COMMAND} -E remove_directory ${PROJECT_SOURCE_DIR}/build/
    COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_SOURCE_DIR}/build/
    COMMENT "-- Cleaning the build directory..."
    VERBATIM)

